CSS 접근 및 수정

✒️ 2025-05-23 17:07 내용 수정


// style property의 특정 css attribute으로 접근
selector.style.fontSize = '28px';
selector.style.backgroundColor = 'yellow';

// 아래처럼 접근할 수도 있다.
selector.style = "color:pink;"
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>Insert title here</title>
	</head>
	<body>
		<p id="p1">CSS 변경하기</p>
		
		<script>
			let target = document.getElementById('p1');
			
			target.style.background = 'lightblue';
			target.style.color = 'red';
			target.style.font = 'bold 30px';
			target.style.border = '2px solid black';
			target.setAttribute('marginLeft', '15px');			
		</script>
	</body>
</html>

DOM 4.png

target.style.cssText = 'color:orange; font-size:15px;';

DOM 6.png